<?php

use Phalcon\Mvc\Model;

class Users extends Model
{

    protected $id;
	protected $name;
	protected $password;
	protected $student_no;
	protected $role;

    public function getSource()
    {
        return "ot_user";
    }

    public function initialize()
    {
        //$this->hasOne("id", "Roles", "role");
        $this->belongsTo("role", "Roles", "id");
        $this->hasMany("id", "Result", "user");
    }
    
    public function onConstruct()
    {
        //$this->role = 1;
    }

    public function getId(){
        return $this->id;
    }

    public function getName()
    {
    	return $this->name;
    }

    public function setName($name)
    {
        if (!preg_match('/^[_0-9a-z]{3,16}$/i', $name) )
        {
            throw new \InvalidArgumentException('用户名应为3到16位数字、字母和下划线组合');
            return;
        }
        $this->name = $name;
    }

    public function setPasswd($password)
    {   
        $password = trim($password);
        if($password == ""){
            throw new \InvalidArgumentException('密码不能为空');
            return;
        }
        $this->password = sha1($password);
    }

    public function setStudent_no($student_no)
    {
        if (!preg_match('/^[0-9]{8,}$/i', $student_no) )
        {
            throw new \InvalidArgumentException('学号应为8位数字');
            return;
        }
        $this->student_no = $student_no;
    }

    public function getStudent_no()
    {
        return $this->student_no;
    }

    public function setRole($role)
    {
        $this->role = $role;
    }

    public function getRole()
    {
        return $this->role;
    }
}